<?php
namespace app\admin\controller;
class Index extends Common{
    function index(){
        $member_id=$this->user['id'];
        // 会员数量
        $members=model('member')->where(['module_id'=>0])->count();
        // 微手会员
        $weishou=model('member')->where(['module_id'=>1])->count();
        // 设备
        $devs=model('device')->count();
        // 账号
        $softs=model('software')->count();
        // 好友（微信客户）
        $socials=model('social')->group('number')->count();
        // 素材
        $sources=model('source')->count();
        $options=[
            'countOption'=>[
                'xAxis'=>['微手会员','米客会员','设备数量','微信账号','好友客户','素材量'],
                'data'=>[$weishou,$members,$devs,$softs,$socials,$sources],
            ],
            'activityOption'=>$this->activityOption(),
        ];
        $this->assign('options',json_encode($options));

        $finance=model('finance')->statis();
        $this->assign('finance',json_encode($finance));
        
        return view();
    }
    /**
     * 用户密码修改
     * @return [type] [description]
     */
    function changePwd(){
        if(request()->isPost()){
            $data=[
                'oldPwd'=>input('oldPwd/s',''),
                'pwd'=>input('pwd/s',''),
                'rpwd'=>input('rpwd/s',''),
            ];
            $validate=$this->validate($data,[
                'oldPwd|'.lang('Old password')=>'require',
                'pwd|'.lang('New password')=>'require',
                'rpwd|'.lang('Duplicate password')=>'require|confirm:pwd',
            ]);
            if(true!==$validate){
                return json($validate,1);
            }
            $user=$this->user;
            $user=model($this->table)->read($user['id']);
            if($user['login_pwd']!==encrypt_pwd($data['oldPwd'])){
                return json(lang('Incorrect username or password'),1);
            }
            $edit=model($this->table)->edit(['id'=>$user['id'],'login_pwd'=>encrypt_pwd($data['pwd'])]);
            if(empty($edit)){
                return json(lang('Update faliure'),1);
            }
            return json([
                'msg'=>lang('Update success'),
                'url'=>url(''),
            ]);
        }
        return view();
    }
    /**
     * 今日活跃度统计（通过用户任务发布统计）
     * @return array
     */
    private function activityOption(){
        $time=strtotime(date('y-m-d'));
        $where=[
            's_time'=>['s_time','between',[$time,$time+86400]]
        ];
        $tasked=model('tasked')->readAll($where,0,['field'=>'member_id']);
        $tasking=model('tasking')->readAll($where,0,['field'=>'member_id']);
        $count=[];
        $ids=[];
        foreach ($tasked as $v) {
            if(isset($count[$v['member_id']])){
                $count[$v['member_id']]++;
            }else{
                $count[$v['member_id']]=1;
            }
            if(!in_array($v['member_id'], $ids)){
                array_push($ids,$v['member_id']);
            }
        }
        foreach ($tasking as $v) {
            if(isset($count[$v['member_id']])){
                $count[$v['member_id']]++;
            }else{
                $count[$v['member_id']]=1;
            }
            if(!in_array($v['member_id'], $ids)){
                array_push($ids,$v['member_id']);
            }
        }
        $rs=[];
        if(count($ids)){
            $map=[
                'id'=>['id','in',$ids]
            ];
            $members=model('member')->readAll($map,0,['field'=>'id,name']);
            $xAxis=[];
            $data=[];
            foreach($members as $v) {
                array_push($xAxis,$v['name']);
                array_push($data,[
                    'value'=>$count[$v['id']],
                    'name'=>$v['name'].'（'.$count[$v['id']].'）',
                ]);
            }
            $rs=[
                'xAxis'=>$xAxis,
                'data'=>$data,
            ];
        }else{
            $rs=[
                'xAxis'=>['今日活跃度'],
                'data'=>[['value'=>0,'name'=>'今日活跃度']],
            ];
        }
        return $rs;
    }
}
